﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LiveNet.Authentication;
using LiveNet.Photos;
using System.Collections.Generic;
using WindowsLive;

namespace DelegatedAuthDemo
{
    public partial class SpacesPhotos : System.Web.UI.Page
    {
        LivePhotos lp;
        protected void Page_Load(object sender, EventArgs e)
        {
            WindowsLiveLogin wll = new WindowsLiveLogin(true);
            WindowsLiveLogin.ConsentToken ct = (WindowsLiveLogin.ConsentToken)Session["ConsentToken"];
            lp = new LivePhotos(ct.LocationID, ct.DelegationToken, AuthenticationToken.DelegatedToken);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Album[] albums = lp.ListAlbums();
            foreach (Album a in albums)
            {
                ListItem item = new ListItem();
                item.Text = a.Name;
                item.Value = a.Id;
                ListBoxAlbums.Items.Add(item);
            }
        }

        protected void ListBoxAlbums_SelectedIndexChanged(object sender, EventArgs e)
        {
            Album album = lp.GetAlbum(ListBox1.SelectedValue);
            foreach (Photo p in album.ListPhotos())
            {
                ListItem item = new ListItem();
                item.Text = string.IsNullOrEmpty(p.Caption) ? "No Caption assigned" : p.Caption;
                item.Value = p.PhotoUrl.ToString();
            }
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label1.Text = ListBox1.SelectedValue;
        }
    }
}
